﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.Supplier
{
    public partial class PlantAllotEdit : PageBase
    {
        PlantBO pbo = new PlantBO();
        PARTBO rbo = new PARTBO();
        LogBO lbo = new LogBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["PID"] != null && Request.QueryString["RID"] != null)
                {
                    LoadPlant(Request.QueryString["RID"].ToString());
                    LoadPart(Request.QueryString["PID"].ToString());
                }
            }
        }

        /// <summary>
        /// 加载工厂
        /// </summary>
        /// <param name="pparid"></param>
        private void LoadPlant(string pparid)
        {
            DataSet ds = pbo.GetList("  p.supplieradmin='" + CurrentUser.Id + "' ");
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlPlant.Items.Add(new ListItem("undistributed", "0"));
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ddlPlant.Items.Add(new ListItem(dr["plantname"].ToString(), dr["id"].ToString()));
                }
            }
            else
            {
                ddlPlant.Items.Add(new ListItem("Not added plant information", "0"));
            }
        }

        private void LoadPart(string plantid)
        {
            Model.PART part = rbo.GetModel(int.Parse(plantid));
            if (part != null)
            {
                if(part.PLANTID!=null)
                ddlPlant.SelectedValue = part.PLANTID.ToString();
                lblPartNo.Text = part.BDNTPARTNO;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["PID"] != null)
            {
                Model.PART part = rbo.GetModel(int.Parse(Request.QueryString["PID"].ToString()));
                if (part != null)
                {
                    part.PLANTID = int.Parse(ddlPlant.SelectedValue);
                    rbo.PlantAllot(part);
                    lbo.WriteLog(CurrentUser.Id, "Part allocation");
                    AlertReflashOpener("Save success");
                }
            }
        }
    }
}